package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityDailyObjectivesBinding;
import com.apex.aolonfitdemo.dialog.MonthDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.GoalCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABleDailyGoal;
import com.cyber.bridge.module.bean.DailyTargetEntity;
import com.cyber.bridge.module.listener.OnCyberQueryAndSetStateListener;


public class DailyObjectivesActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;

    private MonthDialog monthDialog, calorieDialog, distanceDialog, durationDialog;
    private int tempSteps, calorie, distance, duration;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                DailyTargetEntity dailyTargetEntity = (DailyTargetEntity) msg.obj;
                if (dailyTargetEntity == null) {
                    return;
                }
                tempSteps = dailyTargetEntity.getStep();
                calorie = dailyTargetEntity.getCalories();
                activityDailyObjectivesBinding.steps.setText(tempSteps + "");
                activityDailyObjectivesBinding.calorie.setText(calorie + "");
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityDailyObjectivesBinding.steps.setText(tempSteps + "");
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(DailyObjectivesActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityDailyObjectivesBinding activityDailyObjectivesBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityDailyObjectivesBinding = ActivityDailyObjectivesBinding.inflate(getLayoutInflater());
        setContentView(activityDailyObjectivesBinding.getRoot());
        activityDailyObjectivesBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityDailyObjectivesBinding.tool);
        getSupportActionBar().setTitle(null);
        activityDailyObjectivesBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tempSteps = 1;
        calorie = 1;
        AolonFitManager.getInstance().setOnDailyTargetListener(new OnCyberQueryAndSetStateListener<DailyTargetEntity>() {
            @Override
            public void queryConfig(DailyTargetEntity dailyTargetEntity) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.obj = dailyTargetEntity;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setConfig(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(DailyObjectivesActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryDailyTarget();
        activityDailyObjectivesBinding.steps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (monthDialog == null) {
                    monthDialog = new MonthDialog(DailyObjectivesActivity.this, getString(R.string.steps));
                    monthDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            tempSteps = sex;
                            activityDailyObjectivesBinding.steps.setText(sex + "");
                        }
                    });
                    if (!monthDialog.isShowing()) {
                        monthDialog.show();
                    }
                }

            }
        });
        activityDailyObjectivesBinding.calorie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (calorieDialog == null) {
                    calorieDialog = new MonthDialog(DailyObjectivesActivity.this, getString(R.string.calorie));
                    calorieDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            calorie = sex;
                            activityDailyObjectivesBinding.calorie.setText(sex + "");
                        }
                    });
                }
                if (!calorieDialog.isShowing()) {
                    calorieDialog.show();
                }
            }
        });
        activityDailyObjectivesBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DailyObjectivesActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                AolonFitManager.getInstance().setDailyTarget(new DailyTargetEntity(calorie, tempSteps));
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (monthDialog != null) {
            monthDialog.dismiss();
            monthDialog.destroyDialog();
            monthDialog = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (calorieDialog != null) {
            calorieDialog.destroyDialog();
            calorieDialog = null;
        }
        if (distanceDialog != null) {
            distanceDialog.destroyDialog();
            distanceDialog = null;
        }
        if (durationDialog != null) {
            durationDialog.destroyDialog();
            durationDialog = null;
        }
        super.onDestroy();
    }
}
